#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QLabel>
#include "plank.h"
#include <QMouseEvent>
#include "ball.h"
#include <QTimer>
#include <QMessageBox>
#include <QGridLayout>
#include <QList>
#include "brick.h"
#include <QDebug>
#include <QThread>
MainWindow::MainWindow(QWidget *parent):
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setMaximumSize(600,620);setMinimumSize(600,620);
    tim = new QTimer;
    tim->start(10);
    QHBoxLayout qhb1;
    qhbb = &qhb1;
    QLabel* qlaPlank=new QLabel;
    QLabel* qlaBall=new QLabel;
    pl=new Plank(400,400,qlaPlank);
    pl->setPlaLength(188);
    ba = new ball(14,10,526.3,qlaBall,pl);
    qhb1.addWidget(ba->Bla);
    pl->setPlaStyle(3);
    qhb1.addWidget(pl->pla);
    //QGridLayout QGbrick;
    QTimer* timer=new QTimer;
    timer->start(1);
    //double w, double h, double x, double y, int style=1, QLabel * bbr,ball * bba
    for(int r= 0;r<9;r++)
        for(int c=0;c<9;c++)
        {
            QLabel * Templabel = new QLabel;
            brick * bir = new brick(55.0,22.5,30.0+60*c,50.0+24*r,1,Templabel,ba);
            qhb1.addWidget(bir->bbrick);
            bricks.append(bir);
            count ++;
            //qDebug()<<bir->getHeight()<<bir->getWidth()<<bir->getX()<<bir->getY();
            connect(timer,SIGNAL(timeout()),bir,SLOT(PengZhuang()));
            connect(bir,SIGNAL(remove()),this,SLOT(remove()));
        }
    connect(this,SIGNAL(success()),ba,SLOT(end()));
    connect(this,SIGNAL(success()),this,SLOT(end()));
    ui->centralWidget->setLayout(&qhb1);
    //qhb1.addLayout(&QGbrick);
    ui->centralWidget->setMouseTracking(true);
    connect(this,SIGNAL(plankMove(double)),ba,SLOT(moveFollowPlank(double)));
    this->setMouseTracking(true);
    this->pl->pla->setMouseTracking(true);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::remove()
{
    count--;
    //qDebug()<<this->bricks.isEmpty();this->bricks.isEmpty()
    qDebug()<<count;
    if(count == 0)
    {
        emit success();
        guanka = false;
    }
}
void MainWindow::end()
{
    QMessageBox message(QMessageBox::NoIcon, "恭喜过关","您已通过当前关卡，请重新选择关卡");
    message.exec();

}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    if(e->x()<=55)
        pl->pla->setGeometry(0,530,220,40);
    else if(e->x()>=477)
        pl->pla->setGeometry(420,530,220,40);
    else
    pl->pla->setGeometry(e->x()-55,530,220,40);
    emit plankMove(pl->pla->geometry().x());
}
void MainWindow::mousePressEvent(QMouseEvent *e)
{
    if(guanka==false)
    {
        QMessageBox message(QMessageBox::NoIcon, "","请选择关卡");
        message.exec();
    }
    else
    {
        ba->IsStart=true;
        e->x();
        connect(tim,SIGNAL(timeout()),ba,SLOT(moveInBox()),Qt::UniqueConnection);
    }
}

void MainWindow::on_actionChognqian_triggered()
{
    QMessageBox message(QMessageBox::NoIcon, "我要变强","充钱吧,充钱使你变强.微信扫码支付");
    message.setIconPixmap(QPixmap(":/ic/shou"));
    message.exec();
}

void MainWindow::on_action1_triggered()
{
    pl->setPlaStyle(1);
}

void MainWindow::on_action2_triggered()
{
    pl->setPlaStyle(2);
}

void MainWindow::on_action3_triggered()
{
    pl->setPlaStyle(3);
}

void MainWindow::on_action4_triggered()
{
    pl->setPlaStyle(4);
}

void MainWindow::on_action5_triggered()
{
    pl->setPlaStyle(5);
}

void MainWindow::on_action1_2_triggered()
{
    ba->setStyle(1);
}

void MainWindow::on_action2_2_triggered()
{
    ba->setStyle(2);
}

void MainWindow::on_actionkais_triggered()
{
    if(guanka==false)
    {
        QMessageBox message(QMessageBox::NoIcon, "","请选择关卡");
        message.exec();
    }
    if(guanka==true)
    {
        ba->IsStart=true;
        connect(tim,SIGNAL(timeout()),ba,SLOT(moveInBox()));
    }
}

void MainWindow::on_actionguanka_triggered()
{
    guanka = true;
    Guan = 1;
    if(!bricks.isEmpty())
    for(int i = 0;i<bricks.size();i++)
    {
        if(!bricks.at(i)->bbrick->isVisible())
        {
           bricks.at(i)->bbrick->setVisible(true);
           count++;
        }
        if(i%3==0)
        {
            bricks.at(i)->bbrick->setVisible(false);
            count--;
        }
     }
        /*timer=new QTimer;
    timer->start(10);
    for(int r= 0;r<2;r++)
        for(int c=0;c<2;c++)
        {
            QLabel * Templabel = new QLabel(this);
            brick * bir = new brick(60.0,25.0,30.0+60*c,50.0+30*r,1,Templabel,ba);
            //qhbb.addWidget(bir->bbrick);
            //bricks.append(bir);
            //qDebug()<<bir->getHeight()<<bir->getWidth()<<bir->getX()<<bir->getY();
            connect(timer,SIGNAL(timeout()),bir,SLOT(PengZhuang()));
            connect(bir,SIGNAL(remove()),this,SLOT(remove()));
        }*/
}


void MainWindow::on_actionjieshu_triggered()
{
    ba->finish();
}

void MainWindow::on_actionguanka_2_triggered()
{
    guanka = true;
    Guan = 2;
    if(!bricks.isEmpty())
    for(int i = 0;i<bricks.size();i++)
    {
        if(!bricks.at(i)->bbrick->isVisible())
        {
           bricks.at(i)->bbrick->setVisible(true);
           count++;
        }
        if(i%5==0)
        {
            bricks.at(i)->bbrick->setVisible(false);
            count--;
        }
     }
}

void MainWindow::on_actionguanka_3_triggered()
{
    guanka = true;
    Guan = 3;
    if(!bricks.isEmpty())
    for(int i = 0;i<bricks.size();i++)
    {
        if(!bricks.at(i)->bbrick->isVisible())
        {
           bricks.at(i)->bbrick->setVisible(true);
           count++;
        }
        if(i%6==0)
        {
            bricks.at(i)->bbrick->setVisible(false);
            count--;
        }
     }
}

void MainWindow::on_actionguanka_4_triggered()
{
    guanka = true;
    Guan = 4;
    if(!bricks.isEmpty())
    for(int i = 0;i<bricks.size();i++)
    {
        if(!bricks.at(i)->bbrick->isVisible())
        {
           bricks.at(i)->bbrick->setVisible(true);
           count++;
        }
        if(i%7==0||i>34)
        {
            bricks.at(i)->bbrick->setVisible(false);
            count--;
        }
     }
}

void MainWindow::on_actionguanka_5_triggered()
{
    guanka = true;
    Guan = 5;
    if(!bricks.isEmpty())
    for(int i = 0;i<bricks.size();i++)
    {
        if(!bricks.at(i)->bbrick->isVisible())
        {
           bricks.at(i)->bbrick->setVisible(true);
           count++;
        }
        if(i%3==0||i>12)
        {
            bricks.at(i)->bbrick->setVisible(false);
            count--;
        }
     }
}


void MainWindow::on_action_guanka_triggered()
{
    guanka = true;
    Guan = 5;
    if(!bricks.isEmpty())
    for(int i = 0;i<bricks.size();i++)
    {
        if(!bricks.at(i)->bbrick->isVisible())
        {
           bricks.at(i)->bbrick->setVisible(true);
           count++;
        }
        if(i%3==0||i%5==0)
        {
            bricks.at(i)->bbrick->setVisible(false);
            count--;
        }
     }
}

void MainWindow::on_actionguanka_6_triggered()
{
    guanka = true;
    Guan = 5;
    if(!bricks.isEmpty())
    for(int i = 0;i<bricks.size();i++)
    {
        if(!bricks.at(i)->bbrick->isVisible())
        {
           bricks.at(i)->bbrick->setVisible(true);
           count++;
        }
        if(i%2==0||i%7==0)
        {
            bricks.at(i)->bbrick->setVisible(false);
            count--;
        }
     }
}
